package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"net/http"
	"teresa/service"
)

var Deploy deploy

type deploy struct{}

func (d *deploy) GetDeploys(ctx *gin.Context) {
	//处理入参
	params := new(struct {
		FilterName string `form:"filtername"`
		Namespace  string `form:"namespace"`
		Limit      int    `form:"limit"`
		Page       int    `form:"page"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("参数绑定失败", err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败" + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Deploy.GetDeploys(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取deploy列表成功",
		"data": data,
	})
}

func (d *deploy) GetDeployDetail(ctx *gin.Context) {
	params := new(struct {
		DeployName string `form:"deployment"`
		Namespace  string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind绑定参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Deploy.GetDeployDetail(params.DeployName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
	}
	var JsonDeploymentData Jsondeploydata
	jsondeploymentdata, err := json.Marshal(data)
	if err != nil {
		fmt.Println("数据转换失败",err.Error())
		return
	}
	err = json.Unmarshal(jsondeploymentdata, &JsonDeploymentData)
	if err != nil {
		fmt.Println("数据加载失败",err.Error())
		return
	}
	fmt.Println(JsonDeploymentData)
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取deploy详情成功",
		"data": JsonDeploymentData,
	})
}

func (d *deploy) CreateDeployment(ctx *gin.Context) {
	var (
		deployCreate = new(service.DeployCreate)
		err          error
	)
	if err = ctx.ShouldBindJSON(deployCreate); err != nil {
		logger.Error("bind请求参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	if err = service.Deploy.CreateDeployment(deployCreate); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建deploy成功",
		"data": nil,
	})
}

func (d *deploy) UpdateDeployment(ctx *gin.Context) {
	params := new(struct {
		Content   string `json:"content"`
		Namespace string `json:"namespace"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind绑定参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	fmt.Println(params.Content)
	err := service.Deploy.UpdateDeployment(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新deploy成功",
		"data": nil,
	})
}

func (d *deploy) GetDeployNumberNp(ctx *gin.Context) {
	data, err := service.Deploy.GetDeployNumberNp()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取每个namespace的deploy数量成功",
		"data": data,
	})
}

func (d *deploy) GetDeployScale(ctx *gin.Context) {
	params := new(struct {
		DeployName string `form:"deployname"`
		Namespace  string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败",
			"data": nil,
		})
	}
	number, err := service.Deploy.GetDeployScale(params.DeployName, params.Namespace)
	if err != nil {
		if number == 0 {
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"msg":  err.Error(),
				"data": "not found",
			})
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取deploy的副本数成功",
		"data": number,
	})
}

func (d *deploy) UpdateDeployScale(ctx *gin.Context) {
	params := new(struct {
		DeployMent  string `form:"deployment"`
		Namespace   string `form:"namespace"`
		ScaleNumber int32  `form:"scalenumber"`
	})
	if err := ctx.Bind(params); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败",
			"data": nil,
		})
	}
	replicas, err := service.Deploy.UpdateDeployScale(params.DeployMent, params.Namespace, params.ScaleNumber)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "扩缩容成功",
		"data": replicas,
	})
}

func (d *deploy) DeleteDeploy(ctx *gin.Context) {
	params := new(struct {
		DeployName string `form:"deployment"`
		Namespace  string `form:"namespace"`
	})
	err := ctx.Bind(params)
	if err != nil {
		logger.Error("Bind绑定参数失败" + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "Bind绑定参数失败" + err.Error(),
			"data": nil,
		})
		return
	}
	err = service.Deploy.DeleteDeployment(params.DeployName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除deploy成功",
		"data": nil,
	})
}

func (d *deploy) RestartDeployment(ctx *gin.Context) {
	params := new(struct {
		DeployMent string `form:"deployment"`
		Namespace  string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败",
			"data": nil,
		})
	}
	err := service.Deploy.RestartDeployment(params.DeployMent, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "重启成功",
		"data": nil,
	})
}
